package com.feisi.controller;

import com.feisi.domain.Activity;
import com.feisi.domain.User;
import com.feisi.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 查询所有活动()
     *
     * @param request
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(HttpServletRequest request) {
        System.out.println("表现层:查询所有用户");
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        List<Activity> activities = activityService.findAll((page - 1) * limit, limit);
        int count = activityService.activityTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", activities);
        return map;
    }

    /**
     * 根据名称查询活动
     *
     * @param ac_name
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/findByName")
    public @ResponseBody
    Map<String, Object> findByName(String ac_name, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        List<Activity> activities = activityService.findByName(ac_name, (page - 1) * limit, limit);
        int count = activityService.activityTotalByName(ac_name);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", activities);
        return map;
    }

    /**
     * 新增活动
     *
     * @param
     */
    @RequestMapping("/saveActivity")
    public @ResponseBody
    Map<String,Object> saveActivity(String ac_name,String ac_object,String ac_type,String ac_start_time,String ac_end_time,String ac_budget,String ac_site,String desc) {
        Date ac_create_time = new Date();
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Activity ac=new Activity();
        ac.setAc_name(ac_name);
        ac.setAc_object(ac_object);
        ac.setAc_type(ac_type);
        ac.setAc_start_time(ac_start_time);
        ac.setAc_end_time(ac_end_time);
        ac.setAc_budget(Float.parseFloat( ac_budget));
        ac.setAc_site(ac_site);
        ac.setAc_introduction(desc);
        ac.setAc_create_time(df.format(ac_create_time));
        System.out.println("========="+ac);
        activityService.saveActivity(ac);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }

    /**
     * 删除活动
     *
     * @param id
     *//*
    @RequestMapping("/deleteActivity")
    public void deleteActivity(Integer id) {
        activityService.deleteActivity(id);
    }
*/
    /**
     * 根据id删除活动
     * 批量删除
     *
     * @param ids
     *
     */
    @RequestMapping(value = "/deleteActivity",method = {RequestMethod.POST})
    public @ResponseBody
    Map<String, Object> deleteCustomer(@RequestBody String[] ids) {
        Integer[] id = new Integer[ids.length];
        System.out.println(ids.length);
        System.out.println("ids.length====================="+ids.length);
        for (int i=0;i<ids.length;i++){
            System.out.println(ids[i]);
            Integer idOne = Integer.valueOf(ids[i]);
            id[i]=idOne;

        }
        System.out.println(id+"=========================");
        activityService.deleteActivity(id);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }

    /**
     * 修改user
     *
     * @param
     */
    @RequestMapping("/updateActivity")
    public @ResponseBody
    Map<String,Object> updateUser(Integer id, String ac_name, String ac_object, String ac_type, String ac_start_time, String ac_end_time,String ac_budget,String ac_site ,String ac_introduction) {
    Activity ac=new Activity();
    ac.setId(id);
        ac.setAc_name(ac_name);
        ac.setAc_object(ac_object);
        ac.setAc_type(ac_type);
        ac.setAc_start_time(ac_start_time);
        ac.setAc_end_time(ac_end_time);
        ac.setAc_budget(Float.parseFloat( ac_budget));
        ac.setAc_site(ac_site);
        ac.setAc_introduction(ac_introduction);
        System.out.println("修改活动========="+ac);
        activityService.updateActivity(ac);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }


}
